package com.guigu.eduservice.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.Article;
import com.guigu.eduservice.entity.ArticleCategory;
import com.guigu.eduservice.service.ArticleCategoryService;
import com.guigu.eduservice.service.ArticleService;
import com.guigu.eduservice.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 文章表（存储平台发布的资讯、指南等内容） 前端控制器
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/eduservice/article")
public class ArticleController {
    @Autowired
    private ArticleService service;

    @Autowired
    private ArticleCategoryService articleCategoryService;

    //分页查询
    @PostMapping("/getArticlePage/{PageNo}/{PageSize}")
    public PageInfo<Article> getArticlePage(@RequestBody ArticleVo vo,
                                            @PathVariable("PageNo") Integer PageNo,
                                            @PathVariable("PageSize") Integer PageSize){
        PageInfo<Article> articlePage = service.getArticlePage(PageNo, PageSize, vo);
            return  articlePage;
    }

    //根据Id查询
    @GetMapping("/{id}")
    public Article getArticleById(@PathVariable("id") Integer id){
        Article byId = service.getById(id);
        ArticleCategory category = articleCategoryService.getById(byId.getCategoryId());
        byId.setArticleCategories(category);
        return byId;
    }

    //修改
    @PutMapping
    public String UpdateArticle(@RequestBody Article article){


        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setSort(article.getArticleCategories().getSort());
        articleCategory.setCategoryId(article.getArticleCategories().getCategoryId());
        boolean b1 = articleCategoryService.updateById(articleCategory);


        article.setCategoryId(article.getArticleCategories().getCategoryId());
        boolean b = service.updateById(article);
        if(b && b1){
            return "修改成功";
        }
        return "修改失败";
    }

    //添加
    @PostMapping
    public R AddArticle(@RequestBody Article article){
        Article one = new Article();
        one.setCategoryId(article.getArticleCategories().getCategoryId());
        one.setArticleTitle(article.getArticleTitle());
        one.setArticleContent(article.getCoverImage());
        one.setCoverImage(article.getCoverImage());
        one.setPublishStatus(article.getPublishStatus());
        one.setPublishTime(LocalDateTime.now());
        one.setViewCount(0);
        boolean save = service.save(one);
        if(save){
            return R.ok().data("ok","添加成功");
        }
        return R.error().data("no","添加失败");
    }

    //删除
    @DeleteMapping("/{id}")
    public R DeleteArticle ( @PathVariable("id") Integer id){
        boolean b = service.removeById(id);
        if(b){
            return R.ok().data("ok","删除成功");
        }
        return R.error().data("no","删除失败");
    }
}
